package logic

import (
	"context"
	"gorm.io/gorm"
	"three-sxp/models/mysql"

	"three-sxp/orderrpc/internal/svc"
	"three-sxp/orderrpc/orderrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PayLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPayLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PayLogic {
	return &PayLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// Pay todo: 支付宝支付
func (l *PayLogic) Pay(in *orderrpc.PayRequest) (*orderrpc.PayResponse, error) {
	// todo: add your logic here and delete this line
	order, _ := mysql.GetOrderInfo(mysql.Order{
		Model: gorm.Model{ID: uint(in.OrderId)},
	})
	if order.ID == 0 {
		return &orderrpc.PayResponse{
			Code: 400,
			Msg:  "订单不存在",
		}, nil
	}
	order.PayType = "支付宝"
	order.PayStatus = "已支付"
	o, _ := mysql.SaveOrder(order)
	return &orderrpc.PayResponse{
		Code:    200,
		Msg:     "支付成功",
		OrderSn: o.UserSn,
		Money:   float32(o.Money),
	}, nil
}
